package top.z.work.framework.orm.generate;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import top.z.work.framework.common.util.JsonUtil;
import top.z.work.framework.orm.core.BaseEntity;

@Slf4j
public class GeneratorCode {

    public static void main(String[] args) {
        // 配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/top_work?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useTimezone=true&serverTimezone=GMT%2B8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        Generator generator = new Generator(dataSource, createGlobalConfig());
        generator.generate();
    }

    /**
     * {
     * "template": {},
     * "controller": "/templates/enjoy/controller.tpl",
     * "entity": "/templates/enjoy/entityOrBase.tpl",
     * "service": "/templates/enjoy/service.tpl",
     * "mapper": "/templates/enjoy/mapper.tpl",
     * "tableDef": "/templates/enjoy/tableDef.tpl",
     * "mapperXml": "/templates/enjoy/mapperXml.tpl",
     * "serviceImpl": "/templates/enjoy/serviceImpl.tpl"
     * }
     */
    public static GlobalConfig createGlobalConfig() {
        // 创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setEntityJdkVersion(17);
        globalConfig.getTemplateConfig().setEntity("/template/entityOrBase.tpl");

        // 忽略指定列
        globalConfig.getStrategyConfig().setIgnoreColumns("creator", "create_time", "updater", "update_time");

        log.info("{}", JsonUtil.toJsonString(globalConfig.getTemplateConfig()));
        globalConfig.enableEntity()
                .setWithLombok(true)
                .setOverwriteEnable(true)
                .setWithActiveRecord(false)
                .setSuperClass(BaseEntity.class);

        globalConfig.enableTableDef().setOverwriteEnable(true);
        globalConfig.enableMapper().setOverwriteEnable(false);
        globalConfig.disableMapperXml();
        globalConfig.disableService();
        globalConfig.disableServiceImpl();
        globalConfig.disableController();

        // globalConfig.setTablePrefix("system_");
        globalConfig.setSourceDir("D:\\Downloads\\qwer\\yue");
        globalConfig.setBasePackage("top.z.work.module.system");
        globalConfig.setEntityPackage(globalConfig.getBasePackage() + ".entity");
        globalConfig.setMapperPackage(globalConfig.getBasePackage() + ".mapper");
        return globalConfig;
    }
}
